using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using KaroEngine.Exceptions;


namespace KaroGame.FrontEnd
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class TerminalState : DrawableGameComponent
    {
        KaroGame baseGame;
        SpriteBatch spriteBatch;

        SpriteFont font;
        Texture2D png;

        Rectangle sourceRec, destinationRec;

        private string textMessage;

        public TerminalState(Game game)
            : base(game)
        {
            baseGame = game as KaroGame;

            textMessage = "";
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            png = baseGame.Content.Load<Texture2D>("WinnersBar");
            font = baseGame.Content.Load<SpriteFont>("Font/MenuElement");

            sourceRec = new Rectangle(0, 0, png.Width, png.Height);

            spriteBatch = new SpriteBatch(baseGame.GraphicsDevice);

            base.Initialize();
        }
        public void SetWinningPlayer( string playerColor )
        {
            textMessage = playerColor + " Wins";
        }
        public void ResetWinningPlayer()
        {
            textMessage = "";
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            Vector2 size = font.MeasureString( textMessage );
            int xOffset = (int)((baseGame.Window.ClientBounds.Width - size.X) / 2);
            destinationRec = new Rectangle(xOffset, 60, (int)size.X + 20, 50);

            base.Update(gameTime);
        }

        /// <summary>
        /// Allows the game component to draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {
            if ( textMessage.Length > 0 )
            {
                Vector2 pos = new Vector2(destinationRec.X + 10, destinationRec.Y);

                spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);

                spriteBatch.Draw(png, destinationRec, sourceRec, new Color(255, 255, 255, (byte)MathHelper.Clamp( 255, 0, 255 ) ) );
                spriteBatch.DrawString( font, textMessage, pos, new Color( 245, 245, 220, (byte)MathHelper.Clamp( 255, 0, 255 ) ) );

                spriteBatch.End();
            }

            base.Draw(gameTime);
        }
    }
}